package com.zj.apigateway.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

/**
 * @Auther: zj
 * @Date: 2018/12/3 09:25
 * @Description: 跨域配置
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        final CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials( true );//是否支持cookies跨域
        config.setAllowedOrigins( Arrays.asList( "*" ) );//允许原始域，http://www.abc.com
        config.setAllowedHeaders( Arrays.asList( "*" ) );
        config.setAllowedMethods( Arrays.asList( "*" ) );
        config.setMaxAge( 300L );//设置缓存时间

        source.registerCorsConfiguration( "/**", config );
        return new CorsFilter( source );
    }
}
